package com.kt.dao.imp;

import com.kt.dao.OrderDAO;
import com.kt.domain.Order;
import com.kt.utils.UtilDB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDAOImp implements OrderDAO {

    private Order formatOrder(ResultSet resultSet) {
        Order order = null;
        try {
            order = new Order();
            order.setOid(resultSet.getInt("oid"));
            order.setNum(resultSet.getInt("num"));
            order.setPid(resultSet.getInt("pid"));
            order.setPrice(resultSet.getDouble("price"));
            order.setState(resultSet.getInt("state"));
            order.setUid(resultSet.getInt("uid"));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return order;
    }

    public List<Order> findByUid(String uid) {
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "select * from order where uid = ?";
        ResultSet resultSet = null;
        List<Order> list = new ArrayList<Order>();
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, uid);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                list.add(formatOrder(resultSet));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            UtilDB.close(connection);
        }
        return list;
    }

    public Order findById(String oid) {
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "select * from order where oid = ?";
        ResultSet resultSet = null;
        Order order = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, oid);
            resultSet = preparedStatement.executeQuery();
            order = formatOrder(resultSet);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            UtilDB.close(connection);
        }
        return order;
    }

    public int changeOrderInfo(Order order) {
        Connection connection = UtilDB.getConnection();
        PreparedStatement preparedStatement = null;
        String sql = "update order set price=?, num=?, state=?, uid=?, pid=?";
        int result = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setDouble(1, order.getPrice());
            preparedStatement.setInt(2, order.getNum());
            preparedStatement.setInt(3, order.getState());
            preparedStatement.setInt(4, order.getUid());
            preparedStatement.setInt(5, order.getPid());
            result = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            UtilDB.close(connection);
        }
        return result;
    }
}
